<?php

namespace app\common\model\orderdelivery;

use think\Model;
use think\facade\Session;
use app\common\model\user\User;

class Delivery extends Model
{
	// 设置当前模型对应的完整数据表名称
	protected $table = 'delivery';
	protected $pk = 'DeliveryId';

	const STATEOK = 0;
	const STATEINT = 1;

	public function goodInfo()
	{
		return $this->hasOne('app\common\model\orderdata\Goods', 'GoodId', 'GoodId');
	}

	public function info()
	{
		return $this->hasOne('app\common\model\orderdata\Goods', 'GoodId', 'GoodId');
	}


	public function stoarea()
	{
		return $this->hasOne('app\common\model\orderdata\StorageAreaGoods', 'StorageAreaGoodsId', 'StorageAreaGoodsId');
	}

	/**
	 * 获取危废订单
	 * @param  [type] $id [description]
	 * @return [type]     [description]
	 */
	public static function getlist($data, $type = 'ware')
	{
		$ndata =  Delivery::where('DeliveryListId', $data['DeliveryListId'])
					->with(['goodInfo','stoarea','stoarea.areainfo']);
		if ($type == 'ware') {
			$ndata =$ndata->select();
			$hidden = [ 'good_info.OperationLog', 'ReceiptCost', 'ReceiptWeight'];
		} else if ($type == 'acco') {
			$ndata =$ndata->select();
			$hidden = [ 'good_info.OperationLog'];
		} else if ($type == 'mana') {
			$ndata =$ndata->select();
			$hidden = [ 'good_info.OperationLog', 'good_info.TotalWeight'];
		}
		// if(!empty($hidden)){
		// 	$hidden = array_merge([''],$hidden);
		// }
		$ndata->hidden($hidden)->toArray();
		foreach ($ndata as $key => $value) {
			// dump($value);
			$ndata[$key]['GoodName'] = $value['stoarea']['GoodName'];
			$ndata[$key]['GroupName'] = $value['stoarea']['areainfo']['GroupName'];
			$ndata[$key]['Name'] = $value['stoarea']['areainfo']['Name'];
			$ndata[$key]['SelectWeight'] = $value['Weight'];
			$ndata[$key]['Weight'] = $value['stoarea']['Weight'];
			$ndata[$key]['checked'] = true;
			$ndata[$key]['StorageAreaId'] = $value['stoarea']['StorageAreaId'];
			$ndata[$key]['info'] = $value['good_info'];
			$ndata[$key]['text'] = $ndata[$key]['GroupName'] .$ndata[$key]['Name'].'|'.$ndata[$key]['GoodName'];

		}
		return $ndata;
	}

	/**
	 * 增加订单
	 * @param  [type] $Deliverylistid [总订单id]
	 * @param  [type] $goodlist   产品数组
	 * @param  [type] $valuelist  重量数组
	 * @return [type]             [description]
	 */
	public static function screate($DeliveryListId, $goodlist, $valuelist)
	{
		$deletedata = [];
		if (empty($DeliveryListId)) {
			return serr('缺少订单id');
		}
		if (count($goodlist) < count($valuelist)) {
			return serr('订单参数不匹配');
		}
		//输入的数组
		$gdata = [];
		foreach ($goodlist as $key => $value) {
			if (!empty($value)) {
				$gdata[$value] = $valuelist[$key];
			}
		}

		$data = Delivery::where('DeliveryListId', $DeliveryListId)->select();
		//数据的删除与更新
		foreach ($data as $key => $value) {
			//没有从输入列表中找到的记录直接删除
			if (empty($gdata[$value['GoodId']])) {
				$deletedata[] = $value['GoodId'];
			} else {
				if ($value->Weight !== $gdata[$value['GoodId']]) {
					//修改过的数据进行更新
					$value->Weight = $gdata[$value['GoodId']];
					$value->save();
				}
				unset($gdata[$value['GoodId']]);
			}
		}
		//删除数据
		if (!empty($deletedata)) {
			Delivery::where('DeliveryListId', $DeliveryListId)->where('GoodId', 'in', $deletedata)->delete();
		}
		//添加数据
		$cdata = [];
		$ndata['DeliveryListId'] = $DeliveryListId;
		foreach ($gdata as $key => $value) {
			$ndata['GoodId'] = $key;
			$ndata['Weight'] = $value;
			$cdata[] = $ndata;
		}
		$Delivery = new Delivery;
		$data = $Delivery->saveAll($cdata);
		return sok();
	}


	/**
	 * 判断列表中的产品状态是否可操作
	 * @param  [type] $list [id数组]
	 * @return [type]       [返回错误代码]
	 */
	public static function list_state($list)
	{
		$data = Goods::where('State', '<>', Goods::STATEOK)->where('GoodId', 'in', $list)->column('OrderNumber');
		if (!empty($data)) {
			$str = implode("\n", $data);
			$str = "以下危废无法使用：\n" . $str;
			return serr($str);
		} else {
			return sok();
		}
	}


}
